#include <iostream>
#include <vector>
using std::cout;
using std::endl;
using std::vector;

void test(){
    //注意：vector后面跟着一个<int>表示的是当前vector里面存储的数据类型
    vector<int> numbers;

    // vector<int> numbers2 = 10;
    // numbers2里面存储了10个int，数值是默认值0
    vector<int> numbers2(10);
    //numbers3里面存储了10个20
    vector<int> numbers3(10 ,20);

    //这两种方式是完全等价的
    vector<int> numbers4({1, 2, 3, 4, 5, 6, 6});
    vector<int> numbers5 = {1, 2, 3, 4, 6, 8, 8};

    vector<int> numbers6(numbers4.begin(), numbers4.end() - 1);
}

void test2(){
    vector<int> numbers = {1, 2, 3, 4, 6, 8, 8};
    //vector<int>::iterator it = numbers.begin();
    auto it = numbers.begin();
    while(it != numbers.end()){
        cout << *it << endl;
        ++it;
    }
    cout << "===============" << endl;
    cout << numbers.empty() << endl;
    cout << numbers.size() << endl;
    cout << numbers.capacity() << endl;
    numbers.push_back(10);
    auto it2 = numbers.begin();
    while(it2 != numbers.end()){
        cout << *it2 << endl;
        ++it2;
    }
}

void test3(){
    //演示一下size和capacity的区别
    //size:容器中存放的元素的个数
    //capacity:容器中容量
    //vector是动态数组，相较于之前使用 array不需要自行去申请空间
    //扩容机制的，始终是2倍扩容(gcc平台的效果)
    vector<int> numbers;
    cout << numbers.size() << ":" << numbers.capacity() << endl;
    numbers.push_back(1);
    cout << numbers.size() << ":" << numbers.capacity() << endl;
    numbers.push_back(2);
    cout << numbers.size() << ":" << numbers.capacity() << endl;
    numbers.push_back(3);
    cout << numbers.size() << ":" << numbers.capacity() << endl;
    numbers.push_back(4);
    cout << numbers.size() << ":" << numbers.capacity() << endl;
    numbers.push_back(5);
    cout << numbers.size() << ":" << numbers.capacity() << endl;
    numbers.pop_back();
    cout << numbers.size() << ":" << numbers.capacity() << endl;
    numbers.pop_back();
    cout << numbers.size() << ":" << numbers.capacity() << endl;
    //执行之前size=3capacity=8
    numbers.shrink_to_fit();
    //执行之后size=3capacity=3
    cout << numbers.size() << ":" << numbers.capacity() << endl;
    numbers.push_back(9);
    cout << numbers.size() << ":" << numbers.capacity() << endl;
    numbers.clear();
    cout << numbers.size() << ":" << numbers.capacity() << endl;
}

void test4(){
    vector<int> numbers;
    cout << numbers.size() << ":" << numbers.capacity() << endl;
    //预先先申请好足够的空间
    //可以避免后续频繁地进行扩容操作
    numbers.reserve(10);
    cout << numbers.size() << ":" << numbers.capacity() << endl;
}

int main()
{
    test4();
    return 0;
}

